package common.codeGen;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class GenMain {
	private static String path = "E:\\work_phone\\dataBaseTool\\src\\main\\java";

	public static void exec(String packagePath, String tableName,boolean isCreateController) {
		try {

			// String pageStr = "business.news";
			// String tableName = "news";
			// String pageStr = "business.info_release";
			// String tableName = "info_release";

			ApplicationContext ac = new ClassPathXmlApplicationContext(
					"classpath:yeliang/codeGen/gen.xml");
			GenBean genBean = new GenBean();

			String tableName_java = tableName.substring(0, 1).toUpperCase()
					+ tableName.substring(1);
			genBean.setTableName(tableName_java);
			genBean.setPackagePath(packagePath);
			packagePath = packagePath.replace(".", "\\");
			String __path = path + "\\" + packagePath;
			createPackage(__path);

			GenCreateService gs = (GenCreateService) ac
					.getBean("genCreateService");
			String string = gs.create(genBean);

			String serviceName = tableName_java + "Service";
			createJava(__path + "\\service", serviceName, string);

			String dtoName = tableName_java;
			GenCreateDto gd = (GenCreateDto) ac.getBean("genCreateDto");
			String dtoString = gd.create(genBean);
			createJava(__path + "\\dto", dtoName, dtoString);

			String argName = tableName_java + "Argument";
			GenCreateArgument ga = (GenCreateArgument) ac
					.getBean("genCreateArgument");
			String argString = ga.create(genBean);
			createJava(__path + "\\controller", argName, argString);

			String convertName = tableName_java + "Convert";
			GenCreateConvert ca = (GenCreateConvert) ac
					.getBean("genCreateConvert");
			String convertString = ca.create(genBean);
			createJava(__path + "\\service", convertName, convertString);
			if(isCreateController == true){
				 String conName = tableName_java+"Controller";
				 GenCreateController gc=
				 (GenCreateController)ac.getBean("genCreateController");
				 String conString = gc.create(genBean);
				 createJava(__path+"\\controller",conName,conString);
			}

		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		
	}

	public static void createJava(String packagePath, String javaName,
			String string) throws IOException {
		File file3 = new File(packagePath);
		if (file3.exists() == false) {
			file3.mkdirs();
		}
		File file4 = new File(packagePath + "\\" + javaName + ".java");
		if (file4.exists() == true) {
			file4.delete();
		}
		file4.createNewFile();
		FileWriter fw = new FileWriter(file4);
		fw.write(string);
		fw.flush();
		fw.close();
	}

	public static void createPackage(String packagePath) {
		File file = new File(packagePath);
		if (file.exists() == false) {
			file.mkdirs();
		}
		File file2 = new File(packagePath + "\\controller");
		if (file2.exists() == false) {
			file2.mkdirs();
		}
		File file3 = new File(packagePath + "\\service");
		if (file3.exists() == false) {
			file3.mkdirs();
		}
		File file4 = new File(packagePath + "\\dto");
		if (file4.exists() == false) {
			file4.mkdirs();
		}
	}
}
